--
-- Created by IntelliJ IDEA.
-- User: timoo-mac
-- Date: 14-11-6
-- Time: 下午3:13
--

local TipsItem = class("TipsItem");

function TipsItem:ctor()
    self.timeOutIndex = 0;
end

function TipsItem:show(message, color, spriteFrameName, level)
    if color == nil then
        color = text.color1;
    end
    if spriteFrameName == nil then
        spriteFrameName = "bg_img_33.png";
    end

    if self.bg == nil then
        self.bg = CCScale9Sprite:createWithSpriteFrame(CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName(spriteFrameName));
        self.bg:retain();
        self.bg:setInsetTop(10);
        self.bg:setInsetLeft(10);
        self.bg:setInsetRight(10);
        self.bg:setInsetBottom(10);
        self.bg:setAnchorPoint(CCPoint(0.5, 0.5));
        local winSize = CCDirector:sharedDirector():getWinSize();
        self.bg:setPosition(winSize.width / 2, winSize.height / 2);
        self.itemTxt = CCLabelTTF:create(message, "Arial", 25);
        self.itemTxt:retain();
        self.itemTxt:addTo(self.bg);
    end
    self.itemTxt:setString(message);
    self.itemTxt:setFontFillColor(color);
    self.bg:setPreferredSize(CCSizeMake(self.itemTxt:getContentSize().width + 40, 50));
    self.itemTxt:setPosition(self.bg:getContentSize().width / 2, 25);
    self.bg:stopAllActions();
    self.bg:setOpacity(255);
    self.bg:removeFromParent();
    SceneManager:addDisplayToCurrentScene(self.bg, 10);
    if self.timeOutIndex ~= 0 then
        DelayCall:removeTimeOut(self.timeOutIndex);
    end
    self.timeOutIndex = DelayCall:addTimeOut(handler(self, self.hide), 800);
end

function TipsItem:hide()
    self.timeOutIndex = 0;
    if self.bg then
        self.bg:stopAllActions();
        self.bg:runAction(CCSequence:createWithTwoActions(CCFadeOut:create(0.5), CCCallFunc:create(handler(self.bg, self.bg.removeFromParent))));
    end
end

function TipsItem:release()
    if self.bg then
        self.bg:removeFromParent();
        self.bg:release();
        self.bg = nil;
    end
end

return TipsItem;